package com.qianji.petmall.search.controller;

import com.qianji.common.exception.BizCodeEnum;
import com.qianji.common.to.es.SkuEsModel;
import com.qianji.common.utils.R;
import com.qianji.petmall.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author liang zai
 * @date 2022/2/10 12:14
 * @description
 */
@RestController
@RequestMapping("/search")
@Slf4j
public class ProductSaveController {

    @Autowired
    private ProductSaveService productSaveService;

    @PostMapping("/save")
    public R saveProduct(@RequestBody List<SkuEsModel> list){
        boolean status;
        try {
           status = productSaveService.saveProduct(list);
        }catch (Exception e){
            log.error("商品上架异常",e);
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }
        if (status){
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }
        return R.ok();
    }
}
